package IOBaseTest;

import java.io.*;

/**
 * Created by yexuan on 2018/3/13.
 * 字符流一般处理文字数据  字节流还可以处理图片 视频等
 */
public class FileStreamDemo {
    public static void main(String[] args) throws IOException {
        read_method3();
    }

    //若文件过大 推荐使用方法2 此方法可能导致内存溢出
    public static void read_method3() throws IOException {
        FileInputStream fis = new FileInputStream("fos.txt");
        byte[] c = new byte[fis.available()];  //定义一个刚刚好的缓冲区
        fis.read(c);
        System.out.println(new String(c));
        fis.close();
    }

    public static void read_method2() throws IOException {
        FileInputStream fis = new FileInputStream("fos.txt");
        byte[] c = new byte[1024];
        int num = 0;
        while ((num = fis.read(c))!=-1){
            System.out.print(new String(c,0,num));
        }
        fis.close();
    }

    //InputStream  负责读
    public static void read_method1() throws IOException {
        FileInputStream fis = new FileInputStream("fos.txt");
        int num = 0;
        while ((num = fis.read())!=-1){
            System.out.print(num);
        }
        fis.close();
    }

    //outputStream 负责写
    public static void write_method() throws IOException {
        FileOutputStream fos = new FileOutputStream("fos.txt");

        fos.write("xiexieni!".getBytes());
        //无需刷新
        fos.close();
    }
}
